<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<section class="content-header">
    <h1>
        库存审核列表
    </h1>
</section>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" id="adjMatDesc" name="adjMatDesc"
                                           class="form-control"
                                           placeholder="请输入物料描述">
                                </div>
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" id="adjProjectCode" name="adjProjectCode"
                                           class="form-control"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" id="adjProjectName" name="adjProjectName"
                                           class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>跟踪号</label>
                                    <input type="text" id="adjTrackCode" name="adjTrackCode"
                                           class="form-control"
                                           placeholder="请输入跟踪号">
                                </div>
                                <div class="form-group">
                                    <label>
                                        状态
                                    </label>
                                    <select id="adjStatus" name="adjStatus" class="form-control">
                                        <option value="">全部</option>
                                        <option value="3" selected="selected">待审核</option>
                                        <option value="4">审核通过</option>
                                        <option value="5">审核不通过</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>时间</label>
                                    <input type="text" class="form-control datepicker" name="startTime"
                                           readonly="readonly"/>
                                    <span>至</span>
                                    <input type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn_search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="row" id="toolbar">
                    <button shiro:hasPermission="matinv:matadjapp:edit" type="button"
                            class="btn btn-success"
                            id="btn-togetherCommit">
                        批量审核
                    </button>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>

<script th:inline="javascript">

    var table;

    //根据id提交审核通过数据
    function auditpass(adjId) {
        js.modal.confirm("确定审核通过吗？", function () {
            js.post({
                url: ctx + "material/matInvManage/rawMatJusTexaChange/audit",
                data: {
                    adjId: adjId

                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                },
                error: function (result) {
                    js.modal.fail(result.msg);
                }
            })
        })
    }

    //根据id提交审核不通过通过数据
    function auditfailed(adjId) {
        js.modal.confirm("确定审核不通过吗？", function () {
            js.post({
                url: ctx + "material/matInvManage/rawMatJusTexaChange/failed",
                data: {
                    adjId: adjId

                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();

                }
            })
        })
    }


    $(function () {
        /**
         * 加载列表
         */
        table = js.table.init({
            url: ctx + "material/matInvManage/rawMatJusTexaChange/checklist",
            showExport: false,
            onLoadSuccess: function (data) {
                var dataSource = $('#bootstrap-table').bootstrapTable('getData', true);
                mergeCells(dataSource, "cellMergeFlg", 1, $('#bootstrap-table')); //合并单元格
            },
            rowStyle: function (row, index) {
                if (row.adjAfterItyId === 0) {
                    return {css: {"border-top-color": "#333"}}
                } else {
                    return {css: {"border-bottom-color": "#333", "border-bottom": "solid 1px"}}
                }
            },
            columns: [
                {
                    field: "checked",
                    title: '全选',
                    checkbox: true,
                    formatter: function (value, row, index) {
                        if (row.adjStatus === 3) {
                            return {
                                disabled: false
                            }
                        } else {
                            return {
                                disabled: true
                            }
                        }
                    }
                },
                {title: '调整数量', field: 'adjNum', width: '80'},
                {title: '调整重量', field: 'adjWeight', width: '80'},
                {title: '跟踪号', field: 'adjTrackCode', width: '120'},
                {title: '项目编号', field: 'adjProjectCode', width: '100'},
                {title: '物料描述', field: 'adjMatDesc', width: '100'},
                {title: '特殊说明', field: 'adjSpec', width: '150'},
                {title: '项目名称', field: 'adjProjectName', width: '230'},
                {title: '调整原因', field: 'adjReason', width: '200'},
                {
                    title: '在库类型', field: 'adjInType',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '库存';
                        } else if (value === 2) {
                            return '项目';
                        }
                    }
                },
                {
                    title: '调整类型', field: 'adjType', width: '80',
                    formatter: function (value, row, index) {
                        if (row.adjType === 1) {
                            return '<span class="badge label-warning">' + '库存调整' + '</span> ';
                        } else if (row.adjType == 2) {
                            return '<span class="badge label-info">' + '盘点调整' + '</span> ';
                        }
                    }
                },
                {title: '申请人', field: 'adjPerson', width: '70'},
                {title: '申请日期', field: 'adjDate', width: '100'},
                {
                    title: '状态', field: 'adjStatus', width: '100',
                    formatter: function (value, row, index) {
                        if (value === 4) {
                            return '<span class="badge bg-green">审核通过</span> ';
                        } else if (value === 5) {
                            return '<span class="badge bg-red">审核不通过</span> ';
                        } else if (value === 3) {
                            return '<span class="badge bg-yellow">待审核</span> ';
                        }
                    }
                },
                {
                    title: '操作', field: 'adjOdaId', width: '110',
                    formatter: function (value, row, index) {
                        var html = [];
                        if (row.adjStatus == 3 && row.adjOditStatus != 1 && row.adjType != 2) {
                            html.push('<a class="btn btn-info btn-xs" style="margin-right:2px;width: 72px;"  href="' + ctx + 'material/matInvManage/rawMatJusTexaChange/bbb/' + row.adjId + '" >查看</a> ');
                            html.push('<a class="btn btn-success btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditpass(' + row.adjId + ')">审核通过</a> ');
                            html.push('<a class="btn btn-danger btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditfailed(' + row.adjId + ')">审核不通过</a> ');
                        }
                        if (row.adjStatus == 3 && row.adjOditStatus == 1) {
                            html.push('<a class="btn btn-success btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditpass(' + row.adjId + ')">审核通过</a> ');
                            html.push('<a class="btn btn-danger btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditfailed(' + row.adjId + ')">审核不通过</a> ');
                        }
                        if (row.adjStatus == 3 && row.adjType == 2) {
                            html.push('<a class="btn btn-success btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditpass(' + row.adjId + ')">审核通过</a> ');
                            html.push('<a class="btn btn-danger btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditfailed(' + row.adjId + ')">审核不通过</a> ');
                        }
                        if (row.adjStatus == 4 && row.adjType != 2) {
                            html.push('<a class="btn btn-info btn-xs" style="margin-right:2px;width: 72px;"  href="' + ctx + 'material/matInvManage/rawMatJusTexaChange/bbb/' + row.adjId + '" >查看</a> ');
                        }
                        if (row.adjType == 2 && row.adjStatus == 4) {
                            return '';
                        }
                        if (row.adjStatus == 4 && row.adjOditStatus == 1 && row.adjType == 1) {
                            return '';
                        }
                        if (row.adjStatus == 5) {
                            html.push('<a class="btn btn-info btn-xs" style="margin-right:2px;width: 72px;"  href="' + ctx + 'material/matInvManage/rawMatJusTexaChange/bbb/' + row.adjId + '" >查看</a> ');
                            html.push('<a class="btn btn-success btn-xs" style="margin-top: 3px;width: 72px;" href="javascript:void(0)" onclick="auditpass(' + row.adjId + ')">审核通过</a> ');
                        }

                        return html.join('');
                    }
                }

            ]
        });

        /**
         * 合并单元格
         * @param data  原始数据（在服务端完成排序）
         * @param fieldName 合并属性名称
         * @param colspan   合并列
         * @param target    目标表格对象
         */
        function mergeCells(data, fieldName, colspan, target) {
            var valueArry = new Array();//数组只记录值
            for (var i = 0; i < data.length; i++) {
                for (var prop in data[i]) {
                    if (prop == fieldName) {
                        var key = data[i][prop]
                        valueArry[i] = key;
                        break;
                    }
                }
            }

            var numArry = new Array();//记录值出现的次数  一会就通过循环次数组  合并单元格
            var value = valueArry[0];//初始值
            var num = 0;//记录值出现的次数
            var index1 = 0;//numArry的下标
            for (var i = 0; i <= valueArry.length; i++) {
                if (i == valueArry.length) {//解决最后一次赋值
                    numArry[index1] = num;
                    break;
                }
                if (value == valueArry[i]) {
                    num++;//list下一个值与变量value值相同时，num+1，下标不变
                } else {
                    value = valueArry[i];//如果值不相等就把下一个值   赋值给value变量
                    numArry[index1] = num;//把value变量之前的值 出现的次数记录下来
                    num = 1;// 新的value值出现一次
                    index1++;//下标+1;
                }
            }

            var index = 0;
            var fiedArry = ["checked", "adjStatus", "adjOdaId"];
            for (var i = 0; i < numArry.length; i++) {
                var count = numArry[i] * 1;
                fiedArry.forEach(function (field) {
                    $(target).bootstrapTable('mergeCells', {
                        index: index,
                        field: field,
                        colspan: colspan,
                        rowspan: count
                    });
                });
                index += count;
            }
        }

        /** 批量提交 */
        $("#btn-togetherCommit").click(function () {

            var rows = js.table.selectRows("bootstrap-table");

            // 如果没有选择，显示提示信息
            if (rows.length == 0) {
                js.modal.warning("请至少选择一条明细");
                return false;
            } else {
                var arr = js.table.selectColumns(table, "adjId");
                js.post({
                    url: ctx + "material/matInvManage/rawMatJusTexaChange/togetherAudit",
                    contentType: "application/json",
                    data: JSON.stringify(arr),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table);
                        } else {
                            js.modal.warning(result.msg);
                            js.table.refresh(table);
                        }
                        js.modal.closeLoading();
                    }
                })
            }
        });

        /**
         * 表格搜索
         */
        $("#btn_search").click(function () {
            js.table.search(table);
        });

    });
</script>
